package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.leihao.bo.OrderReplyBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.wubang.bo.OrderListBO;
import com.cskaoyan.bean.wubang.bo.OrderRefund;
import com.cskaoyan.bean.wubang.bo.OrderShipBO;
import com.cskaoyan.bean.wubang.vo.OrderDetailVO;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @auther: wubang
 * @description:
 * @date: 2022-06-28 09:16
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;


    @Override
    public CommonData<MarketOrder> selectOrderList(BaseParam baseParam, OrderListBO orderListBO) {
        // 开启分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        // 创建example对象，添加准则
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        if (orderListBO.getUserId() != null) {
            criteria.andUserIdEqualTo(orderListBO.getUserId());
        }

        if (orderListBO.getStart() != null) {
            criteria.andAddTimeGreaterThan(orderListBO.getStart());
        }

        if (orderListBO.getEnd() != null) {
            criteria.andEndTimeLessThan(orderListBO.getEnd());
        }
        if (orderListBO.getOrderStatusArray() != null) {
            criteria.andOrderStatusIn(orderListBO.getOrderStatusArray());
        }
        if (orderListBO.getOrderId() != null) {
            criteria.andIdEqualTo(orderListBO.getOrderId());
        }
        if (orderListBO.getOrderSn() != null) {
            criteria.andOrderSnEqualTo(orderListBO.getOrderSn());
        }
        // 调用mapper查询数据库
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        // 获取分页信息
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        // 封装数据返回结果
        return CommonData.data(pageInfo);

    }

    @Override
    public OrderDetailVO selectOrder(Integer id) {
        OrderDetailVO orderDetailVO = new OrderDetailVO();
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        orderDetailVO.setOrder(marketOrder);

        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketOrder.getUserId());
        AdminInfoBean adminInfoBean = new AdminInfoBean();
        adminInfoBean.setNickName(marketUser.getNickname());
        adminInfoBean.setAvatar(marketUser.getNickname());
        orderDetailVO.setUser(adminInfoBean);

        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        if (id != null) {
            criteria.andOrderIdEqualTo(id);
        }

        List<MarketOrderGoods> marketOrderGoods = this.marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        orderDetailVO.setOrderGoods(marketOrderGoods);
        return orderDetailVO;
    }

    @Override
    public void updateShip(OrderShipBO orderShipBO) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderShipBO.getOrderId());
        marketOrder.setShipChannel(orderShipBO.getShipChannel());
        marketOrder.setShipSn(orderShipBO.getShipSn());
        Date date = new Date(System.currentTimeMillis());
        marketOrder.setShipTime(date);
        marketOrder.setOrderStatus((short) 301);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return;
    }

    @Override
    public void updaterefund(OrderRefund orderRefund) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderRefund.getOrderId());
        marketOrder.setRefundAmount(orderRefund.getRefundMoney());
        Date date = new Date(System.currentTimeMillis());
        marketOrder.setRefundTime(date);
        marketOrder.setOrderStatus((short) 203);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return;
    }

    @Override
    public Boolean reply(OrderReplyBo replyBo) {
        Integer commentId = replyBo.getCommentId();
        String content = replyBo.getContent();
        String adminComment = marketOrderMapper.selectAdminCommentById(commentId);
        if(adminComment==null||"".equals(adminComment)){
            marketOrderMapper.UpdateAdminCommentById(commentId,content);
            return true;
        }
        return false;
    }
}
